package com.select.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.select.course.entity.SchoolCourse;
import com.select.course.entity.StudentCourseRelation;
import com.select.course.entity.UserStudent;
import com.select.course.mapper.SchoolCourseMapper;
import com.select.course.service.*;
import com.select.course.utils.PageUtils;
import com.select.course.vo.CoursePageVo;
import com.select.course.vo.CourseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 24820
* @description 针对表【c_school_course】的数据库操作Service实现
* @createDate 2022-10-28 16:37:35
*/
@Service
public class SchoolCourseServiceImpl extends ServiceImpl<SchoolCourseMapper, SchoolCourse>
    implements SchoolCourseService{

    @Resource
    private TeacherCourseRelationService teacherCourseRelationService;
    @Resource
    private UserTeacherService userTeacherService;
    @Lazy
    @Resource
    private UserStudentService userStudentService;
    @Resource
    private StudentCourseRelationService studentCourseRelationService;

    @Override
    public PageUtils getSelectionAll(CoursePageVo coursePageVo) {

        Page<SchoolCourse> schoolCoursePage = new Page<>(coursePageVo.getPageNum(), coursePageVo.getPageSize());
        QueryWrapper<SchoolCourse> wrapper = new QueryWrapper<>();
        String userId = coursePageVo.getUserId();
        UserStudent userStudent = userStudentService.getOne(new QueryWrapper<UserStudent>().eq("id_number", userId));
        if (!StringUtils.isEmpty(userStudent)) {
            List<StudentCourseRelation> student_id = studentCourseRelationService.list(new QueryWrapper<StudentCourseRelation>().eq("student_id", userStudent.getIdNumber()));
            List<Long> collect1 = student_id.stream().map(e -> {
                Long courrseId = e.getCourseId();
                return courrseId;
            }).collect(Collectors.toList());
            if (collect1 != null && collect1.size() != 0) {
                wrapper.notIn("id", collect1);
            }
        }
        Integer score = coursePageVo.getCourseCore();
        if (!StringUtils.isEmpty(score)) {
            wrapper.eq("course_core",score);
        }
        String courseType = coursePageVo.getCourseType();
        if (!StringUtils.isEmpty(courseType)) {
            if (courseType.equals("选修")) {
                wrapper.eq("course_type",0);

            }else {
                wrapper.eq("course_type",1);

            }
        }
        String query = coursePageVo.getQuery();

        if (!StringUtils.isEmpty(query)) {
            wrapper.eq("course_id",query).or().like("name",query);
        }

        Page<SchoolCourse> page = this.page(schoolCoursePage, wrapper);
        List<SchoolCourse> records = page.getRecords();
        List<CourseVo> collect = records.stream().map(e -> {
            CourseVo courseVo = new CourseVo();
            BeanUtils.copyProperties(e,courseVo);
            CourseVo courseVo1 = userStudentService.packageCourseVo(e, courseVo);
            return courseVo1;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }
}




